<template>
  <div>
    <ul
    class="page-infinite-list"
    v-infinite-scroll="loadMore"
    infinite-scroll-disabled="loading"
    infinite-scroll-distance="50">
      <li
        v-for="item in list"
        :key="item.id" class="page-infinite-listitem"
      >
        <router-link tag="div" :to="`/detail/${item.id}`">
          <img
            v-lazy.container="item.poster.thumbnail"
            :v-lazy="item.poster.thumbnail"
            :alt="item.name">
          <div class="desc">
            <h2>{{item.name}}</h2>
            <p>{{item.intro}}</p>
            <p v-if="item.isNowPlaying">{{item.cinemaCount}}家影院正在热播</p>
            <div class="bottom-bar">
              <div class="grade">
                评分: <mt-badge size="small" type="warning">{{item.grade}} </mt-badge>
              </div>
              <mt-button
                @click.native.stop="addToCart({
                  id: item.id,
                  title: item.name,
                  url: item.poster.origin
                })"
                size="small" type="primary"
              >加入购物车</mt-button>
            </div>
          </div>
        </router-link>
      </li>
      <li v-if="!hasMore">没有更多了……</li>
    </ul>
  </div>
</template>

<script>
import { getGoodsList } from '@/services';
import { mapMutations } from 'vuex';

export default {
  name: 'category',
  data() {
    return {
      loading: false,
      list: [],
      count: 5,
      page: 1,
      hasMore: true,
    };
  },
  methods: {
    ...mapMutations(['addToCart']),
    loadMore() {
      if (!this.hasMore) {
        return false;
      }
      this.loading = true;
      getGoodsList({
        page: this.page,
        count: this.count,
      }).then((resp) => {
        this.list = this.list.concat(resp.films);
        this.page += 1;
        if (resp.page.current === resp.page.total) {
          this.hasMore = false;
        }
        this.loading = false;
      });
      return true;
    },
  },
};
</script>

<style lang="scss" scoped>
  .page-infinite-listitem {
    height: 150px;
    padding: 15px;
    > div {
      display: flex;
      text-decoration: none;
      color: #666;
    }
    img {
      height: 150px;
    }
  }
  image[lazy=loading] {
    height: 150px;
    margin: auto;
  }
  .desc {
    margin-left: 15px;
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    h2 {
      font-size: 18px;
      color: #666;
    }
    p {
      font-size: 14px;
      color: #999;
    }
  }
  .bottom-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
</style>
